package com.iou.view;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.iou.bean.IOUNote;
import com.iou.bean.IOUUser;
import com.iou.database.DatabaseHelper;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Notification;
import com.vaadin.ui.OptionGroup;
import com.vaadin.ui.PopupDateField;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.Window;

/**
 * The "New IOU" window.
 * 
 */
public class WindowView {

    /*
     * Class variables
     */
    private Window mywindow; // The window to be opened
    private static final List<String> types = Arrays.asList(new String[] {
            "IOU", "UOMe" });
    private IOUUser user;
    final OptionGroup noteSelect;
    final TextField amount;
    final TextField participant;
    final PopupDateField date;
    final TextArea description;

    /**
     * Constructor.
     * 
     * @param user
     */
    public WindowView(final IOUUser user) {
        this.user = user;
        mywindow = new Window("New IOU");
        mywindow.center();
        mywindow.setHeight("405px");
        mywindow.setWidth("380px");
        mywindow.setResizable(false);
        mywindow.setModal(true);

        Button savebutton = new Button("Save");

        AbsoluteLayout windowLayout = new AbsoluteLayout();
        windowLayout.setSizeFull();

        mywindow.setContent(windowLayout);

        UI.getCurrent().addWindow(mywindow);

        noteSelect = new OptionGroup("Type:", types);
        noteSelect.setValue("IOU");

        amount = new TextField("Amount:");
        amount.setWidth("50px");

        participant = new TextField("Participant:");
        participant.setWidth("200px");

        date = new PopupDateField("Date:", new Date(System.currentTimeMillis()));

        description = new TextArea("Description:");
        description.setWidth("300px");

        /*
         * Retrieves information from components, creates a new IOU, adds it to
         * the table and refreshes the table.
         */
        savebutton.addClickListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                IOUNote noteToBeSaved = new IOUNote();
                try {
                    noteToBeSaved = new IOUNote(noteSelect.getValue()
                            .toString(), Double.parseDouble(amount.getValue()),
                            participant.getValue(), description.getValue(),
                            date.getValue().getDate(), (date.getValue()
                                    .getMonth() + 1), (date.getValue()
                                    .getYear() + 1900), false);
                    noteToBeSaved.setUser(user);
                    DatabaseHelper.storeNote(noteToBeSaved);
                    ListView.refreshList();
                    ListView.addToTable();
                    Notification.show("New note created!",
                            Notification.Type.TRAY_NOTIFICATION);
                    Window.removeFromParent(mywindow);
                    mywindow.close(); // window is only closed if everything
                                      // went well
                } catch (NumberFormatException e) {
                    Notification.show("Invalid amount!",
                            "Please enter a valid amount, like 3, 256.6 or 0.",
                            Notification.Type.ERROR_MESSAGE);
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        });
        date.setDateFormat("d.M.yyyy");
        /*
         * Addition/alignment
         */
        windowLayout.addComponent(noteSelect, "top:25px; left:10px");
        windowLayout.addComponent(amount, "top:85px; left:10px");
        windowLayout.addComponent(participant, "top:135px; left:10px");
        windowLayout.addComponent(date, "top:185px; left:10px");
        windowLayout.addComponent(description, "top:235px; left:10px");
        windowLayout.addComponent(savebutton, "bottom:10px; right:10px");

    }
}